<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2018~2018 GoMaoMao [ http://www.gomaomao.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.gomaomao.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | 联系方式：GoMaoMao <vip.vvm@qq.com>
// +----------------------------------------------------------------------
// | github开源项目：https://gitee.com/gomaomao.com/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\miniprogram\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 后台入口
 * Class Index
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/15 10:41
 */
class Index extends BasicAdmin
{
	public $table = 'mp_website';

	/**
	 * 小程序站点列表
	 * @return array|string
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\Exception
	 */
	public function index()
	{
		$this->title = '站点管理';
		if (session('user.id') == 10000) {
			$db = Db::name($this->table)->order('id Desc');
		} else {
			$db = Db::name($this->table)->where('user_id', session('user.id'))->order('id Desc');
		}
		return parent::_list($db, false);
	}

	protected function _index_data_filter(&$data)
	{
		if(count($data) == 1){
			session('website',$data[0]['id']);
		}
	}

	/**
	 * 添加
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add()
	{
		return $this->_form($this->table, 'form');
	}

	/**
	 * 编辑
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit()
	{
		return $this->_form($this->table, 'form');
	}

	public function config()
	{
		return $this->_form($this->table, 'config', 'id', [], ['id' => session('website')]);
	}

	function _config_form_filter(&$vo)
	{
		if ($this->request->isGet()) {
//			$vo['banner'] = implode('|', $vo['banner']);
			$vo['banner'] = explode('|', $vo['banner']);
		}
		if ($this->request->isPost()) {
			$vo['banner'] = $vo['banner_0'] . '|' . $vo['banner_1'] . '|' . $vo['banner_2'];
		}
	}

	function _form_filter(&$vo)
	{
		if ($this->request->isPost()) {
			if (!empty($vo['end_at']) && !is_numeric($vo['end_at'])) {
				$vo['end_at'] = strtotime($vo['end_at']);
			}
			if (empty($vo['banner'])) {
				$vo['banner'] = '||';
			}
		}
	}

	/**
	 * 删除站点
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function del()
	{
		if (DataService::update($this->table)) {
			$this->success("站点删除成功！", '');
		}
		$this->error("站点删除失败，请稍候再试！");
	}

	/**
	 * 站点禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function forbid()
	{
		if (DataService::update($this->table)) {
			$this->success("站点禁用成功！", '');
		}
		$this->error("站点禁用失败，请稍候再试！");
	}

	/**
	 * 站点禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function resume()
	{
		if (DataService::update($this->table)) {
			$this->success("站点启用成功！", '');
		}
		$this->error("站点启用失败，请稍候再试！");
	}

	public function user()
	{
		$this->title = '用户列表';
		$db          = Db::name('system_user')->where(['status' => '1']);
		return parent::_list($db->order('id desc'));

	}

	public function session()
	{
		$id = $this->request->get('id', 0);
		if (session('user.id') == 10000) {
			session('website', $id);
			$this->success("站点选择成功!", '');
		}
		$website = Db::name('mp_website')->where('id = ' . $id)->where('user_id = ' . session('user.id'))->find();
		if (empty($website)) {
			$this->error("站点选择失败，请稍候再试！");
		} else {
			session('website', $id);
			$this->success("站点选择成功!", '');
		}
	}
}
